package Arrays;

import java.util.Scanner;

/**
 * 59.螺旋矩阵 ||  11.18
 * 给你一个正整数 n ，生成一个包含 1 到 n2 所有元素，且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
 */
public class generateMatrix {
    public static int[][] generateMatrix(int n) {
        if (n == 1) return new int[][]{new int[]{1}};
        int[][] matrix = new int[n][n];

        int flag = n / 2;
        int count = 1;
        int x1 = 0;
        int y1 = 0;
        int x2 = n-1;
        int y2 = n-1;
        while (flag > 0) {
            for (int i = y1; i < y2; i++) {
                matrix[x1][i] = count++;
            }
            for (int i = x1; i < x2; i++) {
                matrix[i][y2] = count++;
            }
            for (int i = y2; i > y1; i--) {
                matrix[x2][i] = count++;
            }
            for (int i = x2; i > x1; i--) {
                matrix[i][y1] = count++;
            }
            x1++;
            x2--;
            y1++;
            y2--;
            flag--;
        }
        if (n % 2 != 0) {
            matrix[n / 2][n / 2] = count++;
        }
        return matrix;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] ints = generateMatrix(n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(ints[i][j] + " ");
            }
            System.out.println();
        }
    }
}
